﻿using System;
using UnityEngine;

namespace UFrame
{
    public class AndroidSDKHelper
    {
        public static void FuncCall(string methodName, params object[] param)
        {
#if UNITY_ANDROID
            try
            {
                AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
                AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");

                if (jo != null)
                {
                    jo.Call(methodName, param);
                }
            }
            catch (Exception ex)
            {
                string s = string.Empty;
                foreach (var item in param)
                {
                    s += param.ToString() + " type: " + item.GetType().Name + " ";
                }
                PLDebug.LogError("call sdk get exception methodName:" + methodName + " message: " + ex.Message + "\n" + s);
            }
#endif
        }

        public static void FuncStaticCall(string className, string methodName, params object[] param)
        {
#if UNITY_ANDROID
            try
            {
                AndroidJavaClass jc = new AndroidJavaClass(className);
                if (jc != null)
                {
                    jc.CallStatic(methodName, param);
                }
            }
            catch (Exception ex)
            {
                string s = string.Empty;
                foreach (var item in param)
                {
                    s += param.ToString() + " type: " + item.GetType().Name + " ";
                }
                PLDebug.LogError("call sdk  exception class:  " + className + " methodName:" + methodName + " message: " + ex.Message + "\n" + s);
            }
#endif
        }
        
        public static void UIRun(AndroidJavaObject jo, Action call)
        {
            jo.Call("runOnUiThread", new AndroidJavaRunnable(call));
        }

    }
}